package com.analyse.stock.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.analyse.stock.constant.ApiConstant;
import com.analyse.stock.constant.StockConstant;
import com.analyse.stock.mapper.StockCoreInfoMapper;
import com.analyse.stock.model.StockCoreInfoModel;
import com.analyse.stock.model.StockInfoModel;
import com.analyse.stock.service.IStockCoreInfo;
import com.analyse.stock.utils.HttpUtil;
import com.analyse.stock.utils.StockStringUtil;
import com.analyse.stock.vo.FinanceDataVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class StockCoreInfoService extends ServiceImpl<StockCoreInfoMapper, StockCoreInfoModel> implements IStockCoreInfo {

    @Autowired
    private StockInfoService stockInfoService;

    @Override
    public StockCoreInfoModel getStockCoreInfo(String stockCode) {
        return null;
    }

    @Override
    public Integer pullLatestStockInfo(String stockCode) {
        AtomicInteger num = new AtomicInteger(0);
        List<StockInfoModel> list = stockInfoService.getStockList(Sets.newHashSet(stockCode));
        if(CollUtil.isEmpty(list)){
            return 0;
        }
        for(StockInfoModel model : list){
            String secids = String.format("%s.%s",model.getMarkCode(),model.getStockCode());
            String url = StrUtil.format(ApiConstant.STOCK_MV_URL,secids);
            String result = HttpUtil.sendGet(url,"application/javascript", StockConstant.UTF8,6000L);
            if(StringUtils.isBlank(result)){
                continue;
            }
            JSONObject object = JSON.parseObject(result);
            FinanceDataVO dataVO = StockStringUtil.getObject(object.getString("data"), FinanceDataVO.class);
            StockCoreInfoModel coreModel = new StockCoreInfoModel();
            coreModel.setTradeDate(new Date());
            coreModel.setCapitalStock(new BigDecimal(dataVO.getF84()));
            coreModel.setTradeCapitalStock(new BigDecimal(dataVO.getF85()));
            BigDecimal f46 = new BigDecimal(dataVO.getF46()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setOpenPrice(f46);
            BigDecimal f60 = new BigDecimal(dataVO.getF60()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setPreClosePrice(f60);
            BigDecimal f45 = new BigDecimal(dataVO.getF45()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setLowerPrice(f45);
            BigDecimal f44 = new BigDecimal(dataVO.getF44()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setHighPrice(f44);
            BigDecimal f51 = new BigDecimal(dataVO.getF51()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setTallPrice(f51);
            BigDecimal f52 = new BigDecimal(dataVO.getF52()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setLowestPrice(f52);
            BigDecimal f168 = new BigDecimal(dataVO.getF168()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setExchangeRate(f168);
            BigDecimal f50 = new BigDecimal(dataVO.getF50()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setVolumePercent(f50);
            coreModel.setTradeVolume(new BigDecimal(dataVO.getF47()));
            coreModel.setTradeAmount(new BigDecimal(dataVO.getF48()));
            coreModel.setPbRate(new BigDecimal(dataVO.getF167()));
            coreModel.setOuterVolume(new BigDecimal(dataVO.getF49()));
            coreModel.setInnerVolume(new BigDecimal(dataVO.getF161()));
            coreModel.setMarkValue(new BigDecimal(dataVO.getF116()));
            coreModel.setTradeMv(new BigDecimal(dataVO.getF117()));
            BigDecimal f43 = new BigDecimal(dataVO.getF43()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setPrice(f43);
            BigDecimal f170 = new BigDecimal(dataVO.getF170()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setUpDownPercent(f170);
            BigDecimal f169 = new BigDecimal(dataVO.getF169()).divide(new BigDecimal(100),2,BigDecimal.ROUND_UP);
            coreModel.setUpDownAmt(f169);
            coreModel.setUpdateTime(new Date());
            coreModel.setCreateTime(new Date());
            super.save(coreModel);
        }
        return num.get();
    }
}
